/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import DTO.DataColumnDTO;
import DTO.DataTableDTO;
import DTO.DataType;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author TrongKhoa
 */
public class TableDesignModeValues extends AbstractTableModel
{

    public ArrayList<String> _header = new ArrayList<>();
    public ArrayList<DataColumnDTO> _rows = new ArrayList<>();
    public DataTableDTO _table = null;

    public TableDesignModeValues(DataTableDTO table, ArrayList<DataColumnDTO> columns)
    {
        _header.clear();
        _header.add("Tên Cột");
        _header.add("Kiểu dữ liệu");
        _header.add("Not Null");
        _header.add("Khóa chính");
        _rows = columns;
        _table = table;
    }

    @Override
    public int getRowCount()
    {
        return _rows.size();
    }

    @Override
    public int getColumnCount()
    {
        return _header.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex)
    {
        switch (columnIndex)
        {
            case 0:
                return _rows.get(rowIndex).getColumnName();
            case 1:
                return _rows.get(rowIndex).getDataType();
            case 2:
                return _rows.get(rowIndex).isNotNull();
            case 3:
                if (_table.getPrimaryKey().contains(_rows.get(rowIndex)))
                {
                    return true;
                }
                return false;
            default:
                return null;
        }

    }

    @Override
    public String getColumnName(int column)
    {
        return _header.get(column);
    }

    @Override
    public Class<?> getColumnClass(int columnIndex)
    {
        return getValueAt(0, columnIndex).getClass();
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex)
    {
        return true;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex)
    {
        DataColumnDTO column = _rows.get(rowIndex);
        switch (columnIndex)
        {
            case 0:
                column.setColumnName(aValue.toString());
                break;
            case 1:
                column.setDataType((DataType) aValue);
                break;
            case 2:
                column.setNotNull((Boolean) aValue);
                break;
            case 3:
                Boolean b = (Boolean) aValue;
                if (b == true)
                {
                    _table.addPrimaryKey((DataColumnDTO) _rows.get(rowIndex));
                }
                else
                {
                    _table.removePrimaryKey((DataColumnDTO) _rows.get(rowIndex));
                }

                break;
            default:
                break;
        }

        fireTableCellUpdated(rowIndex, columnIndex);
    }
}
